package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 开锁日志
func (*DataParsing) parsingTrapLog(catData []string, ftpInfoId int64) []model.DataTrap {
	var logDate, LogTime, steamId, gameName, gameId, eventType, dataType, owData string
	var trapSteamId, trapGameName, trapGameId, trapSpec, location string
	var end int
	var dataArr []model.DataTrap

	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//开锁类型过滤
		start := strings.Index(logTxt, ": [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "LogTrap" {
			//不是地雷数据跳过
			continue
		}

		//fmt.Println("原始数据", logTxt)
		//开锁触发事件
		start = strings.Index(logTxt, "] ")
		if start < 0 {
			eventType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+2:], ".")
			if end < 0 {
				eventType = ""
			} else {
				eventType = logTxt[start+2 : start+2+end]
			}
		}

		//触发类型数据解析
		if eventType == "Triggered" {
			start = strings.Index(logTxt, "Owner")
			if start < 0 {
				owData = ""
			} else {
				// 查找第二个单引号的位置
				end = strings.Index(logTxt[start+5:], ".")
				if end < 0 {
					owData = ""
				} else {
					owData = logTxt[start+5 : start+5+end]
				}
			}
			//地雷没有归属用户不解析
			if owData != "" {
				start = strings.Index(owData, ": ")
				if start < 0 {
					trapGameName = ""
				} else {
					// 查找第二个单引号的位置
					end = strings.Index(owData[start+2:], " (")
					if end < 0 {
						trapGameName = ""
					} else {
						trapGameName = owData[start+2 : start+2+end]

						trapGameName = TruncateStringByCharCount(trapGameName, 50)

						// 输出字节集（以十六进制表示）
						//fmt.Printf("Byte slice: %x\n", byteSlice)

						// 将字节集转换回字符串
						trapGameName = filterInvalidChars(trapGameName)
					}
				}

				//游戏名id获取
				start = strings.Index(owData, "(")
				if start < 0 {
					trapGameId = ""
					//fmt.Println("找不到单引号")
					//return
				} else {
					// 查找第二个单引号的位置
					end = strings.Index(owData[start+1:], ",")
					if end < 0 {
						trapGameId = ""
						//fmt.Println("找不到第二个单引号")
						//return
					} else {
						// 提取引号内的数据
						trapGameId = owData[start+1 : start+1+end]
					}
				}

				//获取steamId
				start = strings.Index(owData, ", ")
				//fmt.Println("位置1", start)
				if start < 0 {
					trapSteamId = ""
				} else {
					end = strings.Index(owData[start+2:], ")")
					if end < 0 {
						trapSteamId = ""
					} else {
						trapSteamId = owData[start+2 : start+2+end]
					}
				}

			}

		}

		//获取steamId
		start = strings.Index(logTxt, ", ")
		//fmt.Println("位置1", start)
		if start < 0 {
			steamId = ""
		} else {
			end = strings.Index(logTxt[start+2:], ")")
			if end < 0 {
				steamId = ""
			} else {
				steamId = logTxt[start+2 : start+2+end]
			}
		}

		//fmt.Println("steamid", steamId)

		//游戏名名称获取
		start = strings.Index(logTxt, "r: ")
		//fmt.Println("位置1", start)
		if start < 0 {
			gameName = ""
		} else {
			end = strings.Index(logTxt[+start+3:], " (")
			if end < 0 {
				gameName = ""
			} else {
				gameName = logTxt[+start+3 : start+3+end]
				gameName = TruncateStringByCharCount(gameName, 50)

				// 输出字节集（以十六进制表示）
				//fmt.Printf("Byte slice: %x\n", byteSlice)

				// 将字节集转换回字符串
				gameName = filterInvalidChars(gameName)
			}
		}

		//游戏名id获取
		start = strings.Index(logTxt, "(")
		if start < 0 {
			gameId = ""
			//fmt.Println("找不到单引号")
			//return
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+1:], ",")
			if end < 0 {
				gameId = ""
				//fmt.Println("找不到第二个单引号")
				//return
			} else {
				// 提取引号内的数据
				gameId = logTxt[start+1 : start+1+end]
			}
		}

		//类型获取
		start = strings.Index(logTxt, "name: ")
		if start < 0 {
			trapSpec = ""
			//fmt.Println("找不到单引号")
			//return
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+6:], ".")
			if end < 0 {
				trapSpec = ""
				//fmt.Println("找不到第二个单引号")
				//return
			} else {
				// 提取引号内的数据
				trapSpec = logTxt[start+6 : start+6+end]
			}
		}

		start = strings.Index(logTxt, "ion: ")

		if start < 0 {
			location = ""
		} else {
			// 查找第二个单引号的位置
			location = logTxt[start+5:]
		}

		//fmt.Println("数据为：", ftpInfoId, logDate, Time, LogTime, steamId, gameName, gameId, loginType, loginIp, location)

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		trapGameId, _ := strconv.ParseInt(trapGameId, 10, 64)

		downloadFile := model.DataTrap{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, TrapSpec: trapSpec, TrapGameId: trapGameId, TrapSteamId: trapSteamId, TrapGameName: trapGameName, Location: location, EventType: eventType}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}
